package com.atguigu.eduservice.controller;


import com.atguigu.com.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-12-08
 */
@Api(description = "添加课程信息")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService CourseService;

    //课程列表
    @GetMapping("getCourseList")
    public R getCourseList(){
        List<EduCourse> list = CourseService.list(null);
        return R.ok().data("list",list);
    }

    //添加课程信息
    @PostMapping("addCourseInfo")
    public R saveCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回课程id
        String id = CourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }
    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCoutseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo= CourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }
    //修改课程基本信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        CourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }
    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = CourseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }
    //根据id发布课程
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        CourseService.updateById(eduCourse);
        return R.ok();
    }
    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        CourseService.removeCourse(courseId);
        return R.ok();
    }
    //简单的分页方法
    //current 当前页
    //limit   每页显示条数
    @ApiOperation(value = "分页查询课程")
    @GetMapping("pageCourse/{current}/{limit}")
    public R pageCourse(@PathVariable Long current,
                        @PathVariable Long limit){
        Page<EduCourse> eduCoursePage = new Page<>(current,limit);
        IPage<EduCourse> page = CourseService.page(eduCoursePage, null);
        long total = page.getTotal();//总记录数
        List<EduCourse> records = page.getRecords();//集合
        return R.ok().data("total",total).data("records",records);

    }
    //4.条件查询分页方法
    @ApiOperation(value = "条件查询分页方法")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable Long current,
                                 @PathVariable Long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> tPage = new Page<>(current, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String status = courseQuery.getStatus();
        String title = courseQuery.getTitle();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        IPage<EduCourse> page = CourseService.page(tPage, wrapper);
        List<EduCourse> records = page.getRecords();//集合
        long total = page.getTotal();//总记录数
        return R.ok().data("total",total).data("records",records);
    }


}

